package com.the4man.cnbdw.service.impl;

import com.the4man.cnbdw.dao.EmployeeMapper;
import com.the4man.cnbdw.exception.BusinessException;
import com.the4man.cnbdw.service.AbstractService;
import com.the4man.cnbdw.service.EmployeeService;
import com.the4man.cnbdw.utils.Page;
import com.the4man.cnbdw.utils.SelectModel;
import com.the4man.framework.model.Employee;
import com.the4man.framework.utils.ReflectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
@Service("employeeService")
public class EmployeeServiceImpl extends AbstractService<Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    public EmployeeServiceImpl() {
    }

    public void selectStayByExample(Object example, Page page) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        String pojoName = example.getClass().getSimpleName().replaceAll("Example$", "");
        Object mapper = this.getMapper(pojoName);
        int limitStart = (page.getCurPage() - 1) * page.getRowsPerPage();
        if(page.getCurPage() > 0 && page.getRowsPerPage() > 0) {
            List<Class> ptypes = new ArrayList();
            ptypes.add(Integer.TYPE);
            ReflectUtil.invoke(example, "setLimitStart", ptypes, new Object[]{Integer.valueOf(limitStart)});
            ReflectUtil.invoke(example, "setRowsPerPage", ptypes, new Object[]{Integer.valueOf(page.getRowsPerPage())});
        }

        Method method = mapper.getClass().getMethod("selectStayByExample", new Class[]{example.getClass()});
        Object result = method.invoke(mapper, new Object[]{example});
        if(result != null) {
            page.setData((List)result);
        }

        method = mapper.getClass().getMethod("countByExample", new Class[]{example.getClass()});
        result = method.invoke(mapper, new Object[]{example});
        if(result != null) {
            page.setRowCount(Integer.parseInt(result.toString()));
        }

    }

    public void selectStayedByExample(Object example, Page page) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        String pojoName = example.getClass().getSimpleName().replaceAll("Example$", "");
        Object mapper = this.getMapper(pojoName);
        int limitStart = (page.getCurPage() - 1) * page.getRowsPerPage();
        if(page.getCurPage() > 0 && page.getRowsPerPage() > 0) {
            List<Class> ptypes = new ArrayList();
            ptypes.add(Integer.TYPE);
            ReflectUtil.invoke(example, "setLimitStart", ptypes, new Object[]{Integer.valueOf(limitStart)});
            ReflectUtil.invoke(example, "setRowsPerPage", ptypes, new Object[]{Integer.valueOf(page.getRowsPerPage())});
        }

        Method method = mapper.getClass().getMethod("selectStayedByExample", new Class[]{example.getClass()});
        Object result = method.invoke(mapper, new Object[]{example});
        if(result != null) {
            page.setData((List)result);
        }

        method = mapper.getClass().getMethod("countedByExample", new Class[]{example.getClass()});
        result = method.invoke(mapper, new Object[]{example});
        if(result != null) {
            page.setRowCount(Integer.parseInt(result.toString()));
        }

    }

    public List<Employee> selectEmployeeByMac(String mac) throws BusinessException, Exception {
        return this.employeeMapper.selectEmployeeByMac(mac);
    }

    public List<Employee> selectBySfId(Short cardType, String cardNo) {
        return this.employeeMapper.selectBySfId(cardType, cardNo);
    }

    public List<SelectModel> getNameSelectData() {
        return this.employeeMapper.getNameSelectData();
    }

    public List<Employee> getAllDataByCardNum(String cardNum) {
        return this.employeeMapper.getAllDataByCardNum(cardNum);
    }

    public String selectByName(String empId) {
        return this.employeeMapper.selectByName(empId);
    }

    public int selectPunchCount() {
        return this.employeeMapper.selectPunchCount();
    }

    public int selectNoPunchCount() {
        return this.employeeMapper.selectNoPunchCount();
    }

    public int updateStartMonitor(String id) {
        return this.employeeMapper.updateStartMonitor(id);
    }

    public int updateEndMonitor(String id) {
        return this.employeeMapper.updateEndMonitor(id);
    }

    public int insertMonitorEmp(Employee empl) {
        int i = this.employeeMapper.insertMonitorEmp(empl);
        return i;
    }

    public Employee selectMonitorEmpl() {
        return this.employeeMapper.selectMonitorEmpl();
    }

    public int countByCard(Short cardType, String cardNo) {
        return this.employeeMapper.selectByCard(cardType, cardNo);
    }

    public String selectById(String empName) {
        return this.employeeMapper.selectById(empName);
    }

    public Employee selectMBySfId(String id, Short cardType, String cardNo) {
        return this.employeeMapper.selectMBySfId(id, cardType, cardNo);
    }

    public int punchCount(String name) {
        return this.employeeMapper.punchCount(name);
    }

    public List<Employee> selectByPunch(String name) {
        return this.employeeMapper.selectByPunch(name);
    }

    public String selectEmpName(String empId) {
        return this.employeeMapper.selectEmpName(empId);
    }

    public List<Employee> allHaveDeviceEmp() {
        return this.employeeMapper.allHaveDeviceEmp();
    }

    public int selectStayCount() {
        return this.employeeMapper.selectStayCount();
    }
}
